/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m001.ui.controllers;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import ep.libs.interfaces.IModule;
import ep.m001.bl.BL001Controller;
import ep.persistence.controllers.T001UserJpaController;
import ep.persistence.controllers.T002DoctorJpaController;
import ep.persistence.controllers.T003PharmacistJpaController;
import ep.persistence.controllers.T004InsuranceJpaController;
import ep.persistence.entities.T001User;
import ep.persistence.entities.T002Doctor;
import ep.persistence.entities.T003Pharmacist;
import ep.persistence.entities.T004Insurance;
import java.util.Collection;

/**
 *
 * @author Spyros
 */
public class M001Controller {

    private BL001Controller bL001Controller;

    public M001Controller() {
        bL001Controller = new BL001Controller();
    }

    public Short login(String username, String password) {
        return bL001Controller.login(username, password);
    }

    public void logout(IModule module) {
        ((WebApplicationContext) module.getApplication().getContext()).getHttpSession().invalidate();
    }

    public T001User getUserFromUsername(String username) {
        return bL001Controller.getUserFromUsername(username);
    }

    public BeanItem<T001User> getRegisterFormBeanItem(String username) {
        if (username == null) {
            return new BeanItem<T001User>(new T001User());
        } else {
            return new BeanItem<T001User>(bL001Controller.getUserFromUsername(username));
        }
    }

    public BeanItem<T002Doctor> getDoctorFormBeanItem(Item userItem) {
        if (userItem != null) {
            T001User user = (T001User) ((BeanItem) userItem).getBean();
            Collection<T002Doctor> doctors = user.getT002DoctorCollection();
            if (doctors != null && !doctors.isEmpty()) {
                return new BeanItem<T002Doctor>(doctors.iterator().next());
            }
        }
        return new BeanItem<T002Doctor>(new T002Doctor());
    }

    public BeanItem<T003Pharmacist> getPharmacistFormBeanItem(Item userItem) {
        if (userItem != null) {
            T001User user = (T001User) ((BeanItem) userItem).getBean();
            Collection<T003Pharmacist> pharmacists = user.getT003PharmacistCollection();
            if (pharmacists != null && !pharmacists.isEmpty()) {
                return new BeanItem<T003Pharmacist>(pharmacists.iterator().next());
            }
        }
        return new BeanItem<T003Pharmacist>(new T003Pharmacist());
    }

    public Collection<T004Insurance> getInsurances(){
        T004InsuranceJpaController t004InsuranceJpaController = new T004InsuranceJpaController();
        return t004InsuranceJpaController.findT004InsuranceEntities();
    }

    public Boolean registerPharmacist(Item userItem, Item pharmacistItem) {
        try {
            T001User user = (T001User) ((BeanItem) userItem).getBean();
            T003Pharmacist pharmacist = (T003Pharmacist) ((BeanItem) pharmacistItem).getBean();
            saveUser(user);
            pharmacist.setT001User(user);
            T003PharmacistJpaController jpaController = new T003PharmacistJpaController();
            if (pharmacist.getId() == null) {
                jpaController.create(pharmacist);
            } else {
                jpaController.edit(pharmacist);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
            return false;
        }
    }

    public Boolean registerDoctor(Item userItem, Item doctorItem, Collection insurances) {
        try {
            T001User user = (T001User) ((BeanItem) userItem).getBean();
            T002Doctor doctor = (T002Doctor) ((BeanItem) doctorItem).getBean();
            saveUser(user);
            doctor.setT004Insurances(insurances);
            doctor.setT001User(user);
            T002DoctorJpaController jpaController = new T002DoctorJpaController();
            if (doctor.getId() == null) {
                jpaController.create(doctor);
            } else {
                jpaController.edit(doctor);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
            return false;
        }
    }

    private void saveUser(T001User user) throws Exception {
        T001UserJpaController jpaController = new T001UserJpaController();
        if (user.getId() == null) {
            jpaController.create(user);
        } else {
            jpaController.edit(user);
        }
    }
}
